package com.tms.warehouse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class ProfileController extends BaseController {

	private static final String PARAMETER_ID = "entityId";
	private static final String PARAMETER_LENGTH = "length";
	private static final String PARAMETER_COUNT = "count";
	private static final String PARAMETER_NOTICE = "notice";
	private static final String PARAMETER_TYPE = "profileTypeId";
	private static final String PARAMETER_SHELF = "shelfId";
	private static final String PARAMETER_COLOR = "colorId";

	@Override
	protected String doListAction(HttpServletRequest req,
			HttpServletResponse resp) {

		req.setAttribute("entities", Warehouse.getAllProfiles());
		return "./profile.jsp";
	}

	@Override
	protected String doCreateAction(HttpServletRequest req,
			HttpServletResponse resp) {

		req.setAttribute("types", Warehouse.getAllProfileTypes());
		req.setAttribute("colors", Warehouse.getAllColors());
		req.setAttribute("shelves", Warehouse.getAllShelves());
		return "./profile.jsp";
	}

	@Override
	protected String doEditAction(HttpServletRequest req,
			HttpServletResponse resp) {

		String entityId = req.getParameter(PARAMETER_ID);
		req.setAttribute("entity", Warehouse.findProfileById(entityId));
		req.setAttribute("types", Warehouse.getAllProfileTypes());
		req.setAttribute("colors", Warehouse.getAllColors());
		req.setAttribute("shelves", Warehouse.getAllShelves());
		return "./profile.jsp";
	}

	@Override
	protected String doDeleteAction(HttpServletRequest req,
			HttpServletResponse resp) {

		String entityId = req.getParameter(PARAMETER_ID);
		Warehouse.deleteProfile(entityId);
		return TARGET_SELF;

	}

	@Override
	protected String doSaveAction(HttpServletRequest req,
			HttpServletResponse resp) {

		String entityId = req.getParameter(PARAMETER_ID);	
		String length = req.getParameter(PARAMETER_LENGTH);
		String count = req.getParameter(PARAMETER_COUNT);
		String notice = req.getParameter(PARAMETER_NOTICE);
		String profileTypeId = req.getParameter(PARAMETER_TYPE);
		String colorId = req.getParameter(PARAMETER_COLOR);
		String shelfId = req.getParameter(PARAMETER_SHELF);
		Warehouse.persistProfile(entityId, Integer.parseInt(length),
				Integer.parseInt(count), notice, profileTypeId, colorId,
				shelfId);
		return TARGET_SELF;
	}
}
